package main

func main() {
	matrix := [][]int{
		{1, 4, 7, 11, 15},
		{2, 5, 8, 12, 19},
		{3, 6, 9, 16, 22},
		{10, 13, 14, 17, 24},
		{18, 21, 23, 26, 30}}

	println(findNumberIn2DArray(matrix, -1))
}

func findNumberIn2DArray(matrix [][]int, target int) bool {
	if matrix == nil || len(matrix) == 0 || len(matrix[0]) == 0 || matrix[0] == nil {
		return false
	}
	x, y := len(matrix)-1, 0
	for x >= 0 && y < len(matrix[0]) {
		if target > matrix[x][y] {
			y++
		} else if target < matrix[x][y] {
			x--
		} else {
			return true
		}
	}
	return false
}
